//
//  OSCNewsDetail.m
//  OSClient
//
//  Created by Chaosky on 15/10/23.
//  Copyright © 2015年 1000phone. All rights reserved.
//

#import "OSCNewsDetail.h"
#import "Utils.h"

// 如果在类中有对外的属性是一个readonly的，有两种方式可以对该属性赋值：
// 1、通过在匿名类别中添加该属性，并设置为readwrite
// 2、通过该属性的实例变量来访问_xxxx


//// 匿名类别
//@interface OSCNewsDetail()
//
//// 如果对外提供的属性是Readonly，如果想要在实现文件(.m)中访问该属性，需要在匿名类别中添加该属性，并设置该属性为readwrite
//@property (nonatomic, strong, readwrite) NSMutableArray * relatives;
//
//@end

@implementation OSCNewsDetail

- (instancetype)initWithXMLElement:(ONOXMLElement *)element
{
    // 调用父类的初始化方法，将读取部分element中的数据
    if (self = [super initWithXMLElement:element]) {
        self.softwarelink = [[element firstChildWithTag:@"softwarelink"] stringValue];
        self.softwarename = [[element firstChildWithTag:@"softwarename"] stringValue];
        
        // 读出来的数组中装的是relative节点
        NSArray * relativeElements = [element firstChildWithTag:@"relativies"].children;
        // 初始化数组
        if (!_relatives) {
            _relatives = [[NSMutableArray alloc] init];
        }
        
        // 循环遍历解析XMLElement
        for (ONOXMLElement * element in relativeElements) {
            // 解析
            RelativeArticle * relative = [[RelativeArticle alloc] initWithXMLElement:element];
            NSMutableArray * relativeNews = [NSMutableArray array];
            [relativeNews addObject:relative.rtitle];
            [relativeNews addObject:relative.rurl];
            [_relatives addObject:relativeNews];
        }
    }
    return self;
}

- (NSString *)html
{
    if (!_html) {
        NSDictionary *data = @{
                               @"title": [self.title escapeHTML],
                               @"authorID": self.authorid,
                               @"authorName": self.author,
                               @"timeInterval": [[NSDate dateFromString:self.pubDate] timeAgoSinceNow],
                               @"content": self.body,
                               @"softwareLink": self.softwarelink,
                               @"softwareName": self.softwarename,
                               @"relatedInfo": [Utils generateRelativeNewsString:_relatives],
                               };
        
        _html = [Utils HTMLWithData:data usingTemplate:@"article"];
    }
    
    return _html;
}

@end
